package cuishifeng.blog.learn.camel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.SimpleRegistry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class CamelTestDemo {

    public static ProducerTemplate producerTemplate = null;

    public static void main(String[] args) throws Exception {
        MyService myService = new MyService();
        Map<Class<?>, Object> map = new HashMap<>();
        map.put(MyService.class, myService);
        SimpleRegistry registry = new SimpleRegistry();
        registry.put("myService", map);

        CamelContext context = new DefaultCamelContext(registry);
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:ok")
                        .validate(new Predicate() {
                            @Override
                            public boolean matches(Exchange exchange) {
                                System.out.println("Predicate matches");
                                return true;
                            }

                            @Override
                            public void init(CamelContext context) {
                                System.out.println("Predicate init");
                            }

                            @Override
                            public void initPredicate(CamelContext context) {
                                System.out.println("Predicate initPredicate");
                            }
                        })
                        .filter(new Predicate() {
                            @Override
                            public boolean matches(Exchange exchange) {
                                System.out.println("filter matches");
                                return true;
                            }
                        })
                        .choice()
                        .when(new Predicate() {
                            @Override
                            public boolean matches(Exchange exchange) {
                                return true;
                            }
                        })
                        .choice()
                        .when(header("notify").isEqualTo(User.class))
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                Class aClass = exchange.getIn().getHeader("notify", Class.class);
                                RequestBody requestBody = exchange.getIn().getBody(RequestBody.class);
                                Object user = JSONObject.parseObject(requestBody.getJson(),aClass);
                                exchange.getIn().setBody(user);
                            }
                        })
                        .to("bean:myService?method=doSomeThing")
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                Object user = exchange.getIn().getBody(Object.class);
                                ResponseBody responseBody = new ResponseBody();
                                responseBody.setName("response");
                                responseBody.setData(user);
                                responseBody.setJson(JSON.toJSONString(user));
                                exchange.getIn().setBody(responseBody);
                            }
                        })
                        .when(header("notify").isEqualTo("spuChange"))
                        .to("bean:myService?method=doSomeThing2")
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                Object body = exchange.getIn().getBody();
                                ResponseBody responseBody = new ResponseBody();
                                responseBody.setName("response2");
                                responseBody.setData(body);
                                responseBody.setJson(JSON.toJSONString(body));
                                exchange.getIn().setBody(responseBody);
                            }
                        })
                        .endChoice()
                        .end()
                ;
            }
        });

        context.start();

        producerTemplate = context.createProducerTemplate();

        ApiService apiService = new ApiService();
        RequestBody requestBody = new RequestBody();
        User user = new User("hello", 18);
        requestBody.setJson(JSON.toJSONString(user));
        ResponseBody responseBody = apiService.requestSelf(requestBody);
        System.out.println("requestSelf responseBody: " + responseBody);


        TimeUnit.MINUTES.sleep(10);
    }

}
